﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Model;
using System.Data;
using System.Data.OleDb;
namespace DAL
{
    public class ProductDAL
    {
        static string conStr = ConfigurationManager.AppSettings["ConnStr"] + System.Web.HttpContext.Current.Server.MapPath("~/") + ConfigurationManager.AppSettings["dbPath"] + ";";
        public ProductDAL()
        {

        }
        public static bool Add(ProductInfo pi)
        {
            string sql = "insert into product(typeid,name,img,vname,path,click,context,istop,positions,vpath,createdate) values(" + pi.typeid + ",'" + pi.name + "','" + pi.img + "','" + pi.vname + "','" + pi.path + "'," + pi.click + ",'" + pi.context + "'," + pi.istop + "," +pi.positions + ",'" + pi.vpath + "',now())";
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static bool Update(ProductInfo pi)
        {
            string sql = "update product set typeid=" + pi.typeid + ",name='" + pi.name + "',vname='" + pi.vname + "',path='" + pi.path + "',img='" + pi.img + "',click=" + pi.click + ",sort=" + pi.sort + ",context='" + pi.context + "',istop=" + pi.istop + ",positions=" + pi.positions + ",vpath='" + pi.vpath + "' where id=" + pi.id;
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static bool Delete(string ids)
        {
            string sql = "delete from product where id in ("+ids+")";
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static ProductInfo FindOneById(int id)
        {
            OleDbConnection con = new OleDbConnection(conStr);
            string sql = "select * from product where id=" + id;
            OleDbDataReader dr = CommonDAL.ExecuteReader(con, sql);
            ProductInfo pi = new ProductInfo();
            while (dr.Read())
            {
                pi.id = DataConvert.ToInt(dr["id"]);
                pi.typeid = DataConvert.ToInt(dr["typeid"]);
                pi.name = DataConvert.ToString(dr["name"]);
                pi.img = DataConvert.ToString(dr["img"]);
                pi.vname = DataConvert.ToString(dr["vname"]);
                pi.path = DataConvert.ToString(dr["path"]);
                pi.click = DataConvert.ToInt(dr["click"]);
                pi.context = DataConvert.ToString(dr["context"]);
                pi.istop = DataConvert.ToBool(dr["istop"]);
                pi.positions = DataConvert.ToInt(dr["positions"]);
                pi.sort = DataConvert.ToInt(dr["sort"]);
                pi.vpath = DataConvert.ToString(dr["vpath"]);
                pi.createdate = DataConvert.ToDateTime(dr["createdate"]);
            }
            dr.Close();
            con.Close();
            return pi;
        }
        public static List<ProductInfo> FindTop()
        {
            OleDbConnection con = new OleDbConnection(conStr);
            string sql = "select * from product where positions<>0 order by id desc";
            OleDbDataReader dr = CommonDAL.ExecuteReader(con, sql);
            List<ProductInfo> list = new List<ProductInfo>();
            while (dr.Read())
            {
                ProductInfo pi = new ProductInfo();
                pi.id = DataConvert.ToInt(dr["id"]);
                pi.typeid = DataConvert.ToInt(dr["typeid"]);
                pi.name = DataConvert.ToString(dr["name"]);
                pi.img = DataConvert.ToString(dr["img"]);
                pi.vname = DataConvert.ToString(dr["vname"]);
                pi.path = DataConvert.ToString(dr["path"]);
                pi.click = DataConvert.ToInt(dr["click"]);
                pi.context = DataConvert.ToString(dr["context"]);
                pi.istop = DataConvert.ToBool(dr["istop"]);
                pi.positions = DataConvert.ToInt(dr["positions"]);
                pi.vpath = DataConvert.ToString(dr["vpath"]);
                pi.sort = DataConvert.ToInt(dr["sort"]);
                pi.createdate = DataConvert.ToDateTime(dr["createdate"]);
                list.Add(pi);
            }
            dr.Close();
            con.Close();
            return list;
        }
        public static bool Click(int id)
        {
            string sql = "update product set click=click+1 where id=" + id;
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
